package com.eduService.teacher.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.eduService.teacher.entity.EduCourse;
import com.eduService.teacher.entity.EduTeacher;
import com.eduService.teacher.mapper.EduCourseMapper;
import com.eduService.teacher.service.IEduChapterService;
import com.eduService.teacher.service.IEduCourseService;
import com.eduService.teacher.utils.R;
import com.eduService.teacher.vo.course.courseConditionVo;
import com.eduService.teacher.vo.course.courseMes;
import com.eduService.teacher.vo.course.messageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-05-01
 */
@RestController
@RequestMapping("/educourse")
@CrossOrigin
public class EduCourseController {
    @Autowired
    private IEduCourseService iEduCourseService;

    @Autowired
    private IEduChapterService iEduChapterService;

    @Autowired
    EduCourseMapper eduCourseMapper;


    /*
    *
    * 分页查询课程信息
    *
    * */
    @PostMapping("pageCourse/{page}/{limit}")
    public R pageCourse(@PathVariable("page") Long page , @PathVariable("limit") Long limit){
        Page<EduCourse> tpage = new Page<>(page,limit);
        iEduCourseService.page(tpage,null);
        List<EduCourse> records = tpage.getRecords();
        long total = tpage.getTotal();
        return R.ok().data("total",total).data("data",records);
    }

    /*
    *
    * 根据条件进行查询课程数据
    *
    * */
    @PostMapping("querycoursecondition/{page}/{limit}")
    public R queryCourseCondition(@PathVariable("page")Long page,
                                  @PathVariable("limit")Long limit,
                                  @RequestBody courseConditionVo conditionVo){
        Page<EduCourse> tpage = new Page<>(page,limit);
        iEduCourseService.pageQueryCourse(tpage,conditionVo);
        List<EduCourse> records = tpage.getRecords();
        long total = tpage.getTotal();
        return R.ok().data("total",total).data("data",records);


    }

/**
 * 添加课程信息
 * */
    @PostMapping("savecourse")
    public R saveCourse(@RequestBody courseMes courseMes){
        String courseId = iEduCourseService.saveCourse(courseMes);
        return R.ok().data("id",courseId);
    }

    /*
    *
    * 根据ID 查询课程信息
    *
    * */
    @GetMapping("/getACourse/{id}")
    public R getCourseByid(@PathVariable("id") String id){
        courseMes courseInfo = iEduCourseService.getCourseInfo(id);
        return R.ok().data("courseInfo",courseInfo);
    }

    /*
    * 更新课程信息
    * */
    @PostMapping("updateCourse")
    public R updateCourse(@RequestBody courseMes courseMes){
        iEduCourseService.updateCourseInfo(courseMes);
        return R.ok().msg("更新成功！");
    }

    /*
    *
    * 最终发布确认信息
    *
    * */
    @GetMapping("/getMesInfo/{courseId}")
    public R getMesInfo(@PathVariable("courseId") String courseId){
        messageInfo messageInfo = eduCourseMapper.selectMesInfo(courseId);
        return R.ok().data("messageInfo",messageInfo);
    }

    /*
    *删除课程(删除课程是需要把课程里的所有章节，小节都删除)

    * TODO
    * * */
    @DeleteMapping("delCourse/{courseId}")
    public R delCourse(@PathVariable("courseId")String courseId){
    return R.ok();
    }

    /*
    * 最终发布，修改发布状态
    * */
    @PutMapping("advertise/{courseid}")
    public R advertise(@PathVariable("courseid") String courseid){
        EduCourse course = new EduCourse();
        course.setId(courseid);
        course.setStatus("Normal");
        boolean isUpdate = iEduCourseService.updateById(course);
        if (isUpdate){
            return R.ok().msg("更新成功！");
        }else{
            return R.error().msg("更新失败！");
        }
    }


    /**
     * 课程购买成功，根据课程ID增加课程ID的购买次数
     * */
    @GetMapping("/CountAddOne/{courseId}")
    public R paySuccessCount(@PathVariable("courseId") String courseId){
        iEduCourseService.paySuccessCount(courseId);
        return R.ok().msg("该课程购买数+1");
    }


/**
 * todo
 * 统计各课程的售卖情况，用于对客户需求进行分析
 * getAllBuyCount
 * Map存储
 * */


}
